/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pb138;

import com.sun.star.awt.*;
import com.sun.star.lang.EventObject;
import com.sun.star.awt.XActionListener;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.lang.XComponent;
import cz.muni.fi.pb138.exceptions.ConnectionException;
import cz.muni.fi.pb138.exceptions.IllegalDictionaryNameException;
import cz.muni.fi.pb138.exceptions.XmlReadException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author Marek
 */
public class SearchedWordActionListenerImpl implements com.sun.star.awt.XActionListener {

    private int _nCounts = 0;
    private XControlContainer _xControlCont;
    private Manager _mgr;
    private String _searchedWordField = "SearchedWordField";
    private OODebDict2 _parent;
    private XDialog _dialog;
    /**
     * Search button action listener (the button placed in dialog)
     * @param xControlCont
     * @param parent pointer to OODebDict class
     * @param mgr Manager class instance
     * @param searchedWordField Internal name of search word field
     * @param dialog
     */
    public SearchedWordActionListenerImpl(XControlContainer xControlCont, OODebDict2 parent, Manager mgr, String searchedWordField, XDialog dialog) {
        _xControlCont = xControlCont;
        this._mgr = mgr;
        this._searchedWordField = searchedWordField;
        this._dialog = dialog;
        this._parent = parent;
        System.out.println("Creating listener");
    }

    // XEventListener
    public void disposing(EventObject eventObject) {
        _xControlCont = null;
    }

    // XActionListener
    /**
     * Method called when button is clicked. Makes new dialog (with method from OODebDict2 class) and
     * loads info from debdict.
     * @param actionEvent
     */
    public void actionPerformed(ActionEvent actionEvent) {
        Object wordField = _xControlCont.getControl(_searchedWordField);
        XTextComponent xWord = (XTextComponent) UnoRuntime.queryInterface(
                XTextComponent.class, wordField);
        String wordValue = xWord.getText();

        System.out.println("Searching: "+wordValue);
        
        
        String str = "";
        try {
            //TODO:alert
            str = _mgr.getFormattedText(wordValue);
            // dispose the dialog
        } catch (ConnectionException ex) {
            Logger.getLogger(SearchedWordActionListenerImpl.class.getName()).log(Level.SEVERE, null, ex);
            str = "Connection problem";
        } catch (XmlReadException ex) {
            Logger.getLogger(SearchedWordActionListenerImpl.class.getName()).log(Level.SEVERE, null, ex);
            str = "Xml parsing problem";
        } catch (IllegalDictionaryNameException ex) {
            Logger.getLogger(SearchedWordActionListenerImpl.class.getName()).log(Level.SEVERE, null, ex);
            str = "Other problem";
        }
        System.out.println("Neco");
        _dialog.endExecute();
        _parent.makeCheckDialog(str);
    }
}
